<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>汽运主单对账</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span>主单号：</span><input id="keyMasterBillCode" name="keyMasterBillCode" emptyText="请输入主单号"  class="mini-textbox" selectOnFocus="true" required="false" requiredErrorText="主单号不能为空" />
                <span class="separator"></span>
                <span>承运单位:</span>
                <div id="keyOutUnitId" name="keyOutUnitId"
                     class="mini-combobox"
                     style="width:250px;"
                     popupWidth="600" textField="name" valueField="id"
                     allowInput="true" required="false" requiredErrorText="外发单位不能为空"
                     emptyText="承运单位名称">
                    <div property="columns">
                        <div header="单位名称" field="name"></div>
                        <div header="联系人" field="contact"></div>
                        <div header="电话" field="phone"></div>
                        <div header="地址" field="address"></div>
                    </div>
                </div>
                <span class="separator"></span>
                <span>承运方式:</span><input id="keyTransportWayId" name="keyTransportWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                         required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择承运方式"/>
                <span class="separator"></span>
                <span>开单日期：</span>
                <input id="keyOpenMasterBillStartDate" name="keyOpenBillStartDate"  class="mini-datepicker"   format="yyyy-MM-dd"/>
                --
                <input id="keyOpenMasterBillEndDate" name="keyOpenBillEndDate" class="mini-datepicker"  format="yyyy-MM-dd"/>

                <span class="separator"></span>
                <span>总运费结费状态:</span>
                <input id="keyFreightAmountStatusId" name="keyFreightAmountStatusId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择总运费结费状态"/>

                <span class="separator"></span>
                <span>对账状态:</span>
                <input id="keyAccountCheckStatusId" name="keyAccountCheckStatusId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择对账状态"/>

                <div style="margin-top: 5px"></div>
                <% if(so.hasPermission("autoMasterBillReconciliation:query")){%>
                    <a class="mini-button" iconCls="icon-search" onclick="searchBill()">查询</a>
                <% } %>

                <% if(so.hasPermission("autoMasterBillReconciliation:queryCndClear")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("autoMasterBillReconciliation:confirm")){%>
                    <a class="mini-button" iconCls="icon-lock" onclick="accountStatusChange(432)">确认对账</a>
                <% } %>

                <% if(so.hasPermission("autoMasterBillReconciliation:cancel")){%>
                    <a class="mini-button" iconCls="icon-unlock" onclick="accountStatusChange(431)">取消对账</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("autoMasterBillReconciliation:download")){%>
                    <#download_grid id="downloadGrid" theGridId="datagrid1" theFileName="汽运主单对账表"/>
                <% } %>

                <div style="margin-top: 5px"></div>
                <span style="color: red;">如果主单结算状态是已结算，则不能取消对账</span>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="datagrid1" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/finance_manage/reconciliation/queryAutoMasterBillReconciliationList" idField="id"
                 showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
                 multiSelect = "true" allowResize="true"
                 showGroupsSummary="true" showSummaryRow="true" summaryPosition="bottom"
                 allowSortColumn = "true"
                 sortField="createTime"
                 sortOrder="desc"
                 onshowrowdetail="onShowRowDetail"
                 ondrawsummarycell="summaryRight"
            >
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div type="expandcolumn" ></div>
                    <div field="id" width="140" headerAlign="center" align="center" allowSort="true" renderer="showMasterBillRender">汽运主单号</div>
                    <div field="openMasterBillDate" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">开单日期</div>
                    <div field="openMasterBillDepartmentId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"department"}' renderer="commonRender">开单部门</div>
                    <div field="openMasterBillUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">开单人</div>
                    <div field="totalFreightAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">总运费</div>
                    <div field="freightAmountStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">运费结清状态</div>
                    <div field="transportWayId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">承运方式</div>
                    <div field="transportUnitId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"selfOrOutUnit"}' renderer="commonRender">承运单位</div>
                    <div field="transportUnitMasterBillCodeIds" width="140" headerAlign="center" align="center" allowSort="true">承运单位主单号IDS</div>
                    <div field="startCityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">始发站</div>
                    <div field="destCityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目站市</div>

                    <div field="autoLineRoadName" width="140" headerAlign="center" align="center" allowSort="true">线路名称</div>
                    <div field="sendWayId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">派送方式</div>
                    <div field="pickUpPointName" width="140" headerAlign="center" align="center" allowSort="true">提货点名称</div>
                    <div field="pickUpPointPhone" width="140" headerAlign="center" align="center" allowSort="true">提货点电话</div>
                    <div field="pickUpPointAddress" width="140" headerAlign="center" align="center" allowSort="true">提货点地址</div>

                    <div field="licensePlateNumber" width="140" headerAlign="center" align="center" allowSort="true">车牌号</div>
                    <div field="deliveryTime" width="140" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">走货时间</div>
                    <div field="driver" width="140" headerAlign="center" align="center" allowSort="true">驾驶员</div>
                    <div field="driverPhone" width="140" headerAlign="center" align="center" allowSort="true">驾驶员电话</div>
                    <div field="goodsTypeId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>

                    <div field="totalNumber" width="140" headerAlign="center" align="right" allowSort="false" summaryType="sum">总件数</div>
                    <div field="actualWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">实际重量</div>
                    <div field="chargeableWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">计费重量</div>
                    <div field="weightRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">重量费率</div>
                    <div field="volume" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">体积</div>
                    <div field="volumeRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">体积费率</div>

                    <div field="freightAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">运费合计</div>
                    <div field="otherCharges" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">其他费用</div>
                    <div field="otherChargesComment" width="140" headerAlign="center" align="center" allowSort="true">其他费用说明</div>

                    <div field="masterBillPaymentMethodId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">付款方式</div>
                    <div field="dropAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">拉货调整金额</div>
                    <div field="dropStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">拉货状态</div>

                    <div field="accountCheckStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">对账状态</div>

                    <div field="settlementAmount" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">结算金额</div>
                    <div field="settlementStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">结算状态</div>
                    <div field="comment" width="140" headerAlign="center" align="center" allowSort="false">备注</div>

                </div>
            </div>

        </div>

        <!-- 子表 -->
        <div id="detailDiv" style="display:none; margin-left: 50px">
            <div id="detailGrid" class="mini-datagrid" style="width:700px;height:350px;"
                 url="${base}/pages/master_bill_manage/auto/queryNativeBillList">
                <div property="columns">
                    <div field="nativeBillId" width="50" headerAlign="center" align="center" renderer="showNativeBillRender">分单号</div>
                    <div field="loadNumber" width="70" headerAlign="center" align="right" >配载总件数</div>
                    <div field="loadActualWeight" width="70" headerAlign="center" align="right" >配载实际重量</div>
                    <div field="loadChargeableWeight" width="70" headerAlign="center" align="right" >配载计费重量</div>
                    <div field="loadVolume" width="70" headerAlign="center" align="right">配载体积</div>
                    <div field="flightName" width="70" headerAlign="center" align="center" >班次名称</div>
                    <div field="flightDate" width="70" headerAlign="center" align="center" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">班次日期</div>
                </div>
            </div>
        </div>

        <script type="text/javascript">
            mini.parse();

            let keyMasterBillCode = mini.get("keyMasterBillCode");
            let keyTransportWayId = mini.get("keyTransportWayId");
            let keyOpenMasterBillStartDate = mini.get("keyOpenMasterBillStartDate");
            let keyOpenMasterBillEndDate = mini.get("keyOpenMasterBillEndDate");
            let keyFreightAmountStatusId = mini.get("keyFreightAmountStatusId");
            let keyAccountCheckStatusId = mini.get("keyAccountCheckStatusId");

            let grid = mini.get("datagrid1");

            let detailDiv = document.getElementById("detailDiv");
            let detailGrid = mini.get("detailGrid");


            $(function () {
                // 承运方式下拉列表
                let keyTransportWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_WAY");
                keyTransportWayId.load(keyTransportWayIdJsonData);

                // 总运费结费状态
                let keyFreightAmountStatusIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_SETTLEMENT_STATUS");
                keyFreightAmountStatusId.load(keyFreightAmountStatusIdJsonData);

                // 对账状态
                let keyAccountCheckStatusIdJsonDate  = commonRenderSysDicDropListByKey("sysDic","XT_DIC_ACCOUNT_CHECK_STATUS");
                keyAccountCheckStatusId.load(keyAccountCheckStatusIdJsonDate);

                // 加载外发单位下拉列表
                let outUnitIdJsonData = commonRenderDropList("outUnit");
                mini.get("keyOutUnitId").load(outUnitIdJsonData);

                // 加载列表数据
                grid.load();
            });

            /**
             * 清空查询条件
             */
            function clearSearch(){
                let form = new mini.Form("#queryForm");
                form.clear();
            }

            /**
             * 根据查询条件查询分单
             */
            function searchBill(){
                let form = new mini.Form("#queryForm");
                let data = form.getData(true);
                grid.load(data);
            }


            /**
             * 加载明细
             */
            function onShowRowDetail(e){
                let grid = e.sender;
                let row = e.record;

                let td = grid.getRowDetailCellEl(row);
                td.appendChild(detailDiv);

                detailDiv.style.display = "block";

                detailGrid.load({masterBillId: row.id});

            }

            // 展示主单号时显示其超链接
            function showMasterBillRender(e){
                // renderer="showMasterBillRender"

                let masterBillId = e.value;
                let htmlStr = "<a href='javascript:void(0);' onclick='showViewMasterBill(\""+ masterBillId +"\")'>" + masterBillId + "</a>";

                return htmlStr;
            }

            function showViewMasterBill(masterBillId){
                let tabUrl= "${base}/pages/master_bill_manage/auto/showUpdateMasterBill?opType=view&id=" + masterBillId;
                let tabTitle = "汽运主单查看";
                parent.window.addTabToMainPage(tabTitle,tabTitle,tabUrl);
            }


            function showNativeBillRender(e){
                //  renderer="showNativeBillRender"
                let nativeBillId = e.value;
                let htmlStr = "<a href='javascript:void(0);' onclick='showViewNativeBill(\""+ nativeBillId +"\")'>" + nativeBillId + "</a>";
                return htmlStr;
            }

            function showViewNativeBill(nativeBillId){
                let tabUrl="${base}/pages/native_bill_manage/showUpdateBill?opType=view&id=" + nativeBillId;
                parent.window.addTabToMainPage("分单查看","分单查看",tabUrl);
            }


            /**
             * 320,0,结算状态,结算状态,XT_DIC_SETTLEMENT_STATUS
             * 321,320,未结算,未结算,""
             * 322,320,已结算,已结算,""
             *
             *
             * 430,0,分单主单对账状态,分单主单对账状态,XT_DIC_ACCOUNT_CHECK_STATUS
             * 431,430,未对账,未对账,
             * 432,430,已对账,已对账,

             */
            let settlementStatus = {uncleared:321, cleared:322};

            let accountCheckStatus = {noCheck:431, checked:432};

            /**
             * 主单对账
             */
            function accountStatusChange(accountStatus){
                let selectItems = grid.getSelecteds();
                if(selectItems.length==0){
                    showAlert("请选择需要改变对账状态的主单!");
                    return;
                }

                let canAccountCheckIds = "";
                let canNotAccountCheckIds = "";

                if(accountStatus == accountCheckStatus.noCheck){
                    for(let obj of selectItems){
                        if(obj.settlementStatusId == settlementStatus.cleared){
                            canNotAccountCheckIds += obj.id + " ";
                        }
                        else{
                            canAccountCheckIds += "'" + obj.id + "',"
                        }
                    }
                }
                else{
                    for(let obj of selectItems){
                        canAccountCheckIds += "'" + obj.id + "',"
                    }
                }

                if(canNotAccountCheckIds.length>0){
                    canNotAccountCheckIds = canNotAccountCheckIds.substring(0,canNotAccountCheckIds.length-1);
                    showAlert("取消对账状态的主单号【" + canNotAccountCheckIds +  "】中包含已经结算的主单，不能修改其对账状态为未对账!");
                    return;
                }

                let title = accountStatus == accountCheckStatus.checked?"确认对账?":"取消对账?";
                canAccountCheckIds = canAccountCheckIds.substring(0,canAccountCheckIds.length-1);
                // 给出确认提示框
                mini.confirm("确定为主单【" + canAccountCheckIds + "】"+ title, "对账提示",
                    function (action) {
                        if (action == "ok") {
                            let ret = public_getServerData("${base}/pages/master_bill_manage/auto/changeAccountCheckStatus",{billIds:canAccountCheckIds,accountCheckStatusId:accountStatus});
                            showAlert(ret.message,function (){
                                if(ret.ok){
                                    grid.reload();
                                }
                            });
                        }
                    }
                );


            }


            /**
             * 设置底部统计样式
             * @param e
             */
            function summaryRight(e){
                e.cellStyle="text-align:right;color:red;font-size:18px;font-weight:bold";
            }
        </script>
    </body>
</html>